<?php
    abstract class WZ_Model
    {
        protected $_db;
        protected $_model;
        public $_primaryKey;
        public $_stateSave = 'insert';
        
        public function __construct($model, $primaryKey)
        {
            $this->_db = WZ_Utils::db();
            $this->_model = $model;
            $this->_primaryKey = $primaryKey;
        }
        
        public function query($query)
        {
            return $this->_db->requete($q);
        }
        
        public function fetchArray($query)
        {
            return $this->_db->fetchArray($query);
        }
        
        public function fetchArrayOne($query)
        {
            return $this->_db->fetchArray_one($query);
        }
        
        public function fetchRow($query)
        {
            return $this->_db->fetchRow($query);
        }
        
        public function fetchRowOne($query)
        {
            return $this->_db->fetchRow_one($query);
        }
        
        public function lastId()
        {
            return $this->_db->LastId();
        }
        
        public function affectedRows($query)
        {
            return $this->_db->affected_rows($query);
        }
        
        public function showQuery($num = null)
        {
            return $this->_db->voir_requete($num);
        }
        
        public function queryCache($query)
        {
            return $this->_db->mise_en_cache($query);
        }
        
        public function insert($values)
        {
            return $this->_db->inserer($this->_model, $values);
        }
        
        public function update($values, $where = null)
        {
            return $this->_db->update($this->_model, $values, $where);
        }
        
        public function findParentRow($modelName, $where = null)
        {
            $model = WZ_Utils::loadModel($modelName);
            return $model->getObj($where);
        }
        
        public function getObj($where = null)
        {
            if (null === $where) {
                $where = "1";
            }
            $obj = $this->fetchArray("SELECT * FROM $this->_model WHERE $where");
            $this->_stateSave = 'update';
            return $obj;
        }
        
        public function getModelFields()
        {
            return $this->_db->champs_table($this->_model);
        }
        
        public function getModelFieldsTypes()
        {
            return $this->_db->champs_table_type($this->_model);
        }
        
        function save()
        {
            $fields = self::getModelFields();
            
            $values = array();
            
            foreach ($fields as $fieldKey => $fieldName) {
                if (isset($this->$fieldName) && $fieldName <> $this->_primaryKey) {
                    $values[$fieldName] = $this->$fieldName;
                }
            }
            if ($this->_stateSave == 'insert') {
                return $this->insert($values);
            } else if ($this->_stateSave == 'update') {
                return $this->update($values, "$this->_primaryKey = '" . addslashes($this->$this->_primaryKey) . "'");
            }
        }
        
        protected function formatValue($value, $fieldType)
		{
			$mysql_dateformats = array(
				'datetime' => 'Y-m-d H:i:s', 
				'time' => 'H:i:s', 
				'date' => 'Y-m-d'
			);

			if(isset($mysql_dateformats[$fieldType])) {
				$value = date($mysql_dateformats[$fieldType], is_numeric($value) ? $value : strtotime($value));			
			}	
			if($data['indexType'] == 'int') {
				$value = (int)$value;
			}
			return $value;
		}
		
    }
    